//
//  AMUserInfoDataDataModule.m
//  WeiDu_1_0
//
//  Created by foguo-mac-1 on 01/07/16.
//  Copyright (c) 2016 WeiDu Energy Tech. All rights reserved.
//

#import "HOUserInfoDataModule.h"
#import "HOConstants.h"
#import "Logger.h"
#import "HOConstants.h"
#import "Logger.h"
@interface HOUserInfoDataModule()
@property(nonatomic, retain)NSString *token;
@property(nonatomic, retain)NSString *language;
@property(nonatomic, retain)NSString *token4Rongyun;
@end



@implementation HOUserInfoDataModule


- (instancetype)initWithModuleType:(HO_MODULE_TYPE)type
{
	self = [super init];
    if(self)
    {
        moduleType = type;
    }
    return self;
}

- (void)initTestData
{
	self.userInfo.gender = 0;
	self.userInfo.province = @"浙江省";
	self.userInfo.city = @"杭州市";
	self.userInfo.name = @"鲁从伟";
	self.userInfo.companyName = @"硅规科技";
//	self.userInfo.introduce = @"非常强大的一家科技公司，员工遍及全球";
//	self.userInfo.credit = @"43894328u432";
//	self.userInfo.grade = @"东京大学硕士";
//	self.userInfo.age = @"26";
//	self.userInfo.userType = HO_USER_TYPE_LEADER;
}

- (void)didLogin
{
//    LogInf(@"didLogin");
//    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
//    [userDefaults setObject:self.token forKey:kHO_FIELD_Token];
//    [userDefaults synchronize];
}
- (void)logout
{
    self.token = nil;
	[self cleanup];
}

- (HO_USER_TYPE)getUserType
{
	NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
	HO_USER_TYPE userType = [[userDefaults objectForKey:kHO_FIELD_UserType] intValue];
	self.userInfo.userType = userType;
	return userType;
}
- (NSString *)getToken
{
	if(self.token || self.token.length > 0)
	{
		return self.token;
	}
	NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
	self.token = [userDefaults objectForKey:kHO_FIELD_Token];
	return self.token;
}

- (NSString *)getRongyunToken
{
	if(self.token4Rongyun)
	{
		return self.token4Rongyun;
	}
	NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
	self.token4Rongyun = [userDefaults objectForKey:kHO_FIELD_TokenRongyun];
	return self.token4Rongyun;
}

- (void)saveUserType:(HO_USER_TYPE)userType
{
	NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
	[userDefaults setObject:[NSNumber numberWithInt:userType] forKey:kHO_FIELD_UserType];
	[userDefaults synchronize];
	self.userInfo.userType = userType;
}

- (void)saveToken:(NSString *)token
{
	[self saveAppToken:token];
}

- (void)saveToken:(NSString *)token
 withRongyunToken:(NSString *)rongyunToken
	 withUserType:(HO_USER_TYPE)userType
{
	[self saveAppToken:token];
	[self saveRongyunToken:rongyunToken];
	[self saveUserType:userType];
}

- (void)saveAppToken:(NSString *)token
{
	self.token = token;
	if(self.token != nil)
	{
		NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
		[userDefaults setObject:self.token forKey:kHO_FIELD_Token];
		[userDefaults synchronize];
	}
}

- (void)saveRongyunToken:(NSString *)rongyunToken
{
	self.token4Rongyun = rongyunToken;
	if(self.token4Rongyun != nil)
	{
		NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
		[userDefaults setObject:self.token4Rongyun forKey:kHO_FIELD_TokenRongyun];
		[userDefaults synchronize];
		
	}
}

- (void)saveUserId:(int)userId
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:[NSNumber numberWithInt:userId] forKey:kHO_FIELD_UserId];
    [userDefaults synchronize];
}
- (int)getUserId
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    return [[userDefaults objectForKey:kHO_FIELD_UserId] intValue];
}

- (void)cleanup
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults removeObjectForKey:kHO_FIELD_Token];
    [userDefaults synchronize];
    self.token = nil;
}

- (void)saveLanguage:(NSString *)language
{
	self.language = language;
	NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
	[userDefaults setObject:self.language forKey:kHO_FIELD_Language];
	[userDefaults synchronize];
}

#pragma mark - -- did receive response

- (void)didReceiveResponse:(HOBaseResponseEntity *)response
{
	if([response.action isEqualToString:kHO_ACTION_BankCardBind])
	{
		self.userInfo.bankAccount = [response.baseRequest paramForKey:kHO_FIELD_CardNumber];
	}
	else if([response.action isEqualToString:kHO_ACTION_MobileBind])
	{ 
		self.self.userInfo.telPhone = [response.baseRequest paramForKey:kHO_FIELD_Mobile];
	}
    else if([response.action isEqualToString:kHO_ACTION_Login]
            || [response.action isEqualToString:kHO_FIELD_Mobile])
    {
		self.token = [response.dataNode objectForKey:kHO_FIELD_Token];
		self.token4Rongyun = [response.dataNode objectForKey:kHO_FIELD_TokenRongyun];
//        LogInf(@"token:%@", token);
    }
}

- (void)dealloc
{
    self.token = nil;
	self.userInfo = nil;
    [super dealloc];
}

@end
